package bb.lanxing.lib.devices.base.converter;

import android.bluetooth.BluetoothGattCharacteristic;
import bb.lanxing.lib.devices.base.CharacteristicConverter;
import bb.lanxing.lib.devices.core.ble.IGattClient;
import bb.lanxing.lib.devices.ble.BLEAttributes;
import android.util.Log;
import java.math.BigDecimal;
import java.util.UUID;

public class RowerConverter implements CharacteristicConverter {
    final static private String TAG = "RowerConverter";
    final static private boolean DebugFTMS = false;
    static private int intValueAvgStrokeRate = 0 ;
    static private double doubleValueTotalDistance = 0;
    static private double doubleValuePace = 0;
    static private double doubleValueAvgPace = 0;
    static private int intValuePower = 0;
    static private int intValueAvgPower = 0;
    static private int intValueResistanceLevel = 0;

    public void onRower(int i, int i2, int i3, int i4, double i5, double i6,int i7 , int i8,int i9) {
    }

    public int unsignedByteToInt(byte b) {
        return b & 0xFF;
    }

    public int unsignedBytesToInt24(byte b0, byte b1, byte b2) {
        return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8))
                + (unsignedByteToInt(b2) << 16) ;
    }
    @Override
    public final void convert(IGattClient iGattClient, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        int intValueFlag1byte = 0;
        int intValueFlag2byte = 0;
        int intValueStrokeRate = 0;
        int intValueStrokeCount = 0;
        int intValueEnergy = 0;

        if (bluetoothGattCharacteristic.getUuid().equals(UUID.fromString(BLEAttributes.BLE_FTMS_CHAR_ROWER_DATA ))) {
            intValueFlag1byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
            intValueFlag2byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
            if(DebugFTMS) {
                intValueStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))/2;
                intValueStrokeCount = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                intValueAvgStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))/2;
                doubleValueTotalDistance = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))*1000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                doubleValuePace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))/60).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)/10;
                intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                onRower(iGattClient.getType(), intValueStrokeRate,intValueStrokeCount,intValueAvgStrokeRate, doubleValueTotalDistance,doubleValuePace,intValuePower,intValueResistanceLevel,intValueEnergy);
            }else if((intValueFlag1byte == 174) && (intValueFlag2byte == 11)) { //0xAE 0x0B rower DKN-RW700-00066
                intValueStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 2))/2;
                intValueStrokeCount = (int)bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 3);
                intValueAvgStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 5))/2;
                byte[] value = bluetoothGattCharacteristic.getValue();
                doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[6],value[7],value[8]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                doubleValuePace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 9))/30).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 11);
                intValueResistanceLevel = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 13));
                intValueEnergy = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 15));
                onRower(iGattClient.getType(),intValueStrokeRate,intValueStrokeCount,intValueAvgStrokeRate, doubleValueTotalDistance,doubleValuePace,intValuePower,intValueResistanceLevel,intValueEnergy);
            }else if((intValueFlag1byte == 255) && (intValueFlag2byte == 10)) { //0xFF 0x0A rower Concept2 Part1
                intValueAvgStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 2))/2;
                byte[] value = bluetoothGattCharacteristic.getValue();
                doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[3],value[4],value[5]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                doubleValuePace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 6))/30).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                doubleValueAvgPace = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 8))/30).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 10);
                intValueAvgPower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 12);
                intValueResistanceLevel = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 14));
            }else if((intValueFlag1byte == 0) && (intValueFlag2byte == 1)) { //0x00 0x01 rower ower Concept2 Part2
                intValueStrokeRate = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 2))/2;
                intValueStrokeCount = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 3);
                intValueEnergy = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 5));
                onRower(iGattClient.getType(),intValueStrokeRate,intValueStrokeCount,intValueAvgStrokeRate, doubleValueTotalDistance,doubleValuePace,intValuePower,intValueResistanceLevel,intValueEnergy);
            }
        }
    }
}
